/**
 * 
 */
package com.EstateManager.Dao;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder.In;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.EstateManager.Entity.Leaseorsalerecords;
import com.EstateManager.Entity.User;

/**
 * 创建时间 : 下午2:26:02
 * @author Yonggang Liu
 *
 * LeaseOrSaleRecold.java
 */
@Repository("leaseOrSaleRecoldDao")
public class LeaseOrSaleRecoldDao implements IModelBasicOperate<LeaseOrSaleRecoldDao, Integer> {

	/**
	 * 
	 */
	public LeaseOrSaleRecoldDao() {
	}

	
	
	//合同状态
	public final static String STATUE_EXECUTE = "进行中";
	
	public final static String STATUE_END = "合约到期终止";
	
	public final static String STATUE_CANCELED = "合约已取消";
	
	//租售类型
	public final static String TYPE_SALE = "出租";
	
	public final static String TYPE_LEASE = "出售";
	
	
	@Autowired
    private SessionFactory sessionFactory;	
	
	private Session getCurrentsSession() {
		return sessionFactory.getCurrentSession();
	}
	
	/* (non-Javadoc)
	 * @see com.EstateManager.Dao.IModelBasicOperate#load(java.io.Serializable)
	 */
	@Override
	public LeaseOrSaleRecoldDao load(Integer id) throws HibernateException {
		return (LeaseOrSaleRecoldDao)this.getCurrentsSession().load(LeaseOrSaleRecoldDao.class, id);
	}

	/* (non-Javadoc)
	 * @see com.EstateManager.Dao.IModelBasicOperate#get(java.io.Serializable)
	 */
	@Override
	public LeaseOrSaleRecoldDao get(Integer id) throws HibernateException {
		return (LeaseOrSaleRecoldDao)this.getCurrentsSession().get(LeaseOrSaleRecoldDao.class, id);
	}

	/* (non-Javadoc)
	 * @see com.EstateManager.Dao.IModelBasicOperate#persist(java.lang.Object)
	 */
	@Override
	public void persist(LeaseOrSaleRecoldDao entity) throws HibernateException {
			this.getCurrentsSession().persist(entity);
	}

	/* (non-Javadoc)
	 * @see com.EstateManager.Dao.IModelBasicOperate#save(java.lang.Object)
	 */
	@Override
	public Integer save(LeaseOrSaleRecoldDao entity) throws HibernateException {
		return (Integer)this.getCurrentsSession().save(entity);
	}

	/* (non-Javadoc)
	 * @see com.EstateManager.Dao.IModelBasicOperate#saveOrUpdate(java.lang.Object)
	 */
	@Override
	public void saveOrUpdate(LeaseOrSaleRecoldDao entity) throws HibernateException {
		this.getCurrentsSession().saveOrUpdate(entity);
	}

	/* (non-Javadoc)
	 * @see com.EstateManager.Dao.IModelBasicOperate#detete(java.lang.Object)
	 */
	@Override
	public void detete(LeaseOrSaleRecoldDao entity) throws HibernateException {
		this.getCurrentsSession().delete(entity);
	}

	/* (non-Javadoc)
	 * @see com.EstateManager.Dao.IModelBasicOperate#flush()
	 */
	@Override
	public void flush() throws HibernateException {
		this.getCurrentsSession().flush();
	}

	/* (non-Javadoc)
	 * @see com.EstateManager.Dao.IModelBasicOperate#update(java.lang.Object)
	 */
	@Override
	public void update(LeaseOrSaleRecoldDao entity) throws HibernateException {
		this.getCurrentsSession().update(entity);
	}

	/* (non-Javadoc)
	 * @see com.EstateManager.Dao.IModelBasicOperate#merge(java.lang.Object)
	 */
	@Override
	public void merge(LeaseOrSaleRecoldDao entity) throws HibernateException {
		this.getCurrentsSession().merge(entity);
	}
	
	
	/**
	 * 根据所以人Id获取租售记录
	 * @param ownerId
	 * @param pageSize
	 * @param pageNum
	 * */
	public List<Leaseorsalerecords> getRecoldsByOwnerId(int ownerId, int pageSize, int pageNum) {
		Query query = this.getCurrentsSession().createQuery(""
				+ "select leaseOrSaleRecold "
				+ "from LeaseOrSaleRecold leaseOrSaleRecold "
				+ "where leaseOrSaleRecold.userByOwnerId = :owner");
		User owner = new User();
		owner.setId(ownerId);
		query.setParameter("owner", owner);
		return (List<Leaseorsalerecords>)query
				.setMaxResults(pageSize)
				.setFirstResult((pageNum - 1)*pageSize)
				.list();
				
	}
	
	
	
	
	
	
	
	

}
